const dragStore = useDrag();
export let typeList = {
  formItem: "表单",
  container: "容器",
  tableItem: "表格",
};

export function cancelFn() {
  dragStore.renderData.map((item) => {
    if (item.click) item.click = false;
    if (item.type == "container" && item.childrens)
      item.childrens.map((ite) => {
        if (ite.click) ite.click = false;
      });
  });
}
export function importJSON(doc: string) {
  try {
    dragStore.allReset();
    dragStore.renderData = JSON.parse(doc);
    cancelFn();
    setTimeout(() => {
      dragStore.renderData.map((item) => {
       let current=dragStore.refList?.[item?.highEdit?.ref as keyof typeof dragStore.refList]
        if ( item.key == "diologForm") ( current as ISudEx).dataed.methods.showDialogFn();
        setTimeout(() => {
          if (item.typed == "containerForm") {
            item.childrens.map((ite) => {
              let parent = dragStore.renderData.find((itex) => {
                return itex.id == ite.pid;
              });
              if (!parent) return;
              let parentForm = (dragStore.refList[parent.props.ref as keyof typeof dragStore.refList] as ISuEx).dataed.formRef.form;
              if (ite.props?.itemKey) {
                parentForm[ite.props.itemKey] = ite.highEdit?.itemValue;
              }
              ite.methods.emitHasChange = ({ e }: { e: any }) => {
                if (parentForm) {
                  parentForm[e.key] = e;
                }
              };
          });
            item.methods.emitChangeProps = (obj: { propsName: string[]; e: any }) => {
              let data = item.props[item.key];
              obj.propsName.map((ite: string, index: number) => {
                if (index != obj.propsName.length - 1) {
                  data = data[ite];
                }
              });
              data[obj.propsName.length - 1] = obj.e;
            };
          }
          if ( (item.key == "searchForm" || item.key == "cTable") && item.highEdit.emitToOpen) {
            let dioRef = dragStore?.refList?.[item.highEdit.emitToOpen] as ISudEx ;
            if (!dioRef) return ElMessage.error("组件对应的ref值的弹窗不存在");
            item.methods.emitToOpen = ({ row }: any) => {
               dioRef.dataed.methods.showDialogFn(row ? row : {});
            };
          }
          if ( (item.key == "searchForm" || item.key == "diologForm") && item.highEdit.emitGetListFn) {
            let searchFormKey  = item.key == "searchForm" ? item.highEdit.ref : item.highEdit.searchFormKey;
            let tableRef = dragStore?.refList?.[item.highEdit.emitGetListFn] as ITableEx;
            if (!tableRef) return ElMessage.error("搜索组件对应的ref值的表格不存在");
            item.methods.emitGetListFn = (fn: { fn: () => void }) => {
              tableRef.emitGetListFn(searchFormKey ? (dragStore?.refList[searchFormKey] as ISusEx).dataed.formRef.form : {});
              if (fn?.fn) fn.fn();
            };
          }
        }, 500);
      }, 200);
    });
  } catch (error) {
    ElMessage.error("JSON格式错误");
  }
}
